<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
          <el-form-item label="数据源编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="数据源编码"></el-input>
      </el-form-item>
          <el-form-item label="数据源名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="数据源名称"></el-input>
      </el-form-item>
          <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
          <el-form-item label="数据库类型" prop="dbType">
        <el-input v-model="dataForm.dbType" placeholder="数据库类型"></el-input>
      </el-form-item>
          <el-form-item label="驱动类" prop="dbDriver">
        <el-input v-model="dataForm.dbDriver" placeholder="驱动类"></el-input>
      </el-form-item>
          <el-form-item label="数据源地址" prop="dbUrl">
        <el-input v-model="dataForm.dbUrl" placeholder="数据源地址"></el-input>
      </el-form-item>
          <el-form-item label="数据库名称" prop="dbName">
        <el-input v-model="dataForm.dbName" placeholder="数据库名称"></el-input>
      </el-form-item>
          <el-form-item label="用户名" prop="dbUsername">
        <el-input v-model="dataForm.dbUsername" placeholder="用户名"></el-input>
      </el-form-item>
          <el-form-item label="密码" prop="dbPassword">
        <el-input v-model="dataForm.dbPassword" placeholder="密码"></el-input>
      </el-form-item>
          <el-form-item label="创建人" prop="createBy">
        <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
      </el-form-item>
          <el-form-item label="创建日期" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建日期"></el-input>
      </el-form-item>
          <el-form-item label="更新人" prop="updateBy">
        <el-input v-model="dataForm.updateBy" placeholder="更新人"></el-input>
      </el-form-item>
          <el-form-item label="更新日期" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="更新日期"></el-input>
      </el-form-item>
      </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: '',  code: '',  name: '',  remark: '',  dbType: '',  dbDriver: '',  dbUrl: '',  dbName: '',  dbUsername: '',  dbPassword: '',  createBy: '',  createTime: '',  updateBy: '',  updateTime: ''});

const rules = ref({
          code: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          name: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ]
    //       remark: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       dbType: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       dbDriver: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       dbUrl: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       dbName: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       dbUsername: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       dbPassword: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       createBy: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       createTime: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       updateBy: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       updateTime: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ]
  });

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/oatools/datasource/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/oatools/datasource", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
